﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBEngine.Elements;

namespace FilingSystem
{
	public partial class FilingSystemForm : Form
	{
		public FilingSystemForm()
		{
			InitializeComponent();
		}

		private void FilingSystemForm_Load(object sender, EventArgs e)
		{
            // TODO: This line of code loads data into the '_CVC_DatabaseDataSet_Form.LensesOrder' table. You can move, or remove it, as needed.
           // this.lensesOrderTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.LensesOrder);
            // TODO: This line of code loads data into the '_CVC_DatabaseDataSet_Form.LensesExamination' table. You can move, or remove it, as needed.
           // this.lensesExaminationTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.LensesExamination);
            // TODO: This line of code loads data into the '_CVC_DatabaseDataSet_Form.GlassesOrder' table. You can move, or remove it, as needed.
          //  this.glassesOrderTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesOrder);
            //fillGlassesOrderGridView();
            // TODO: This line of code loads data into the '_CVC_DatabaseDataSet_Form.GlassesExamination' table. You can move, or remove it, as needed.
			//this.glassesExaminationTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesExamination);
            //fillGlassesExaminationGridView();
            // TODO: This line of code loads data into the '_CVC_DatabaseDataSet_Form.ClientInfo' table. You can move, or remove it, as needed.
			this.clientInfoTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.ClientInfo);

		}

		
		private void clientsInfoDataGridView_SelectionChanged(object sender, System.EventArgs e)
		{

			if (clientsInfoDataGridView.Rows.Count < 1)
			{
                updateClientControls(false);
			}
			else
			{
				Int32 selectedRowCount =
				clientsInfoDataGridView.Rows.GetRowCount(DataGridViewElementStates.Selected);
				if (selectedRowCount > 0)
				{
					this.mSelectedClientRow = this.clientsInfoDataGridView.SelectedRows[0];
					this.mSelectedClientInfo = ParseClientInfo(this.mSelectedClientRow);
                    updateClientControls(true);
                   
				}
				else
                {
                    updateClientControls(false);
				}
			}
		}

        private void glassesExaminationDataGridView_SelectionChanged(object sender, System.EventArgs e)
        {

            if (glassesExaminationDataGridView.Rows.Count < 1)
            {
                updateGlassesExaminationControls(false);
            }
            else
            {
                Int32 selectedRowCount =
                glassesExaminationDataGridView.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    this.mSelectedGlassesExaminationRow = this.glassesExaminationDataGridView.SelectedRows[0];
                    this.mSelectedGlassesExamination = ParseGlassesExamination(this.mSelectedGlassesExaminationRow);
                    updateGlassesExaminationControls(true);
                }
                else
                {
                    updateGlassesExaminationControls(false);
                }

            }
        }

        private void glassesOrderDataGridView_SelectionChanged(object sender, System.EventArgs e)
		{

            if (glassesOrderDataGridView.Rows.Count < 1)
			{
                updateGlassesOrderControls(false);
			}
			else
			{
				Int32 selectedRowCount =
                glassesOrderDataGridView.Rows.GetRowCount(DataGridViewElementStates.Selected);
				if (selectedRowCount > 0)
				{
                    this.mSelectedGlassesOrderRow = this.glassesOrderDataGridView.SelectedRows[0];
                    this.mSelectedGlassesOrder = ParseGlassesOrder(this.mSelectedGlassesOrderRow);

                    updateGlassesOrderControls(true);                  
				}
				else
				{
                    updateGlassesOrderControls(false);
				}
			}

		}

        private void lensesExaminationDataGridView_SelectionChanged(object sender, System.EventArgs e)
        {

            if (lensesExaminationDataGridView.Rows.Count < 1)
            {
                updateLensesExaminationControls(false);
            }
            else
            {
                Int32 selectedRowCount =
                lensesExaminationDataGridView.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    this.mSelectedLensesExaminationRow = this.lensesExaminationDataGridView.SelectedRows[0];
                    this.mSelectedLensesExamination = ParseLensesExamination(this.mSelectedLensesExaminationRow);
                    updateLensesExaminationControls(true);
                }
                else
                {
                    updateLensesExaminationControls(false);
                }

            }
        }

        private void lensesOrderDataGridView_SelectionChanged(object sender, System.EventArgs e)
        {

            if (lensesOrderDataGridView.Rows.Count < 1)
            {
                updateLensesOrderControls(false);
            }
            else
            {
                Int32 selectedRowCount =
                lensesOrderDataGridView.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    this.mSelectedLensesOrderRow = this.lensesOrderDataGridView.SelectedRows[0];
                    this.mSelectedLensesOrder = ParseLensesOrder(this.mSelectedLensesOrderRow);

                    updateLensesOrderControls(true);
                }
                else
                {
                    updateLensesOrderControls(false);
                }
            }

        }
       
        private void updateClientInfoButton_Click(object sender, EventArgs e)
        {


            int updatedRowIndex = this.mSelectedClientRow.Index;
            updateSelectedClientInfo(this.mSelectedClientInfo);
            mDBManager.Update(this.mSelectedClientInfo);

            this.clientInfoTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.ClientInfo);
            this.clientsInfoDataGridView.ClearSelection();
            this.clientsInfoDataGridView.Rows[updatedRowIndex].Selected = true;


        }

        private void updateGlassesExaminationButton_Click(object sender, EventArgs e)
        {
            int updatedRowIndex = this.mSelectedGlassesExaminationRow.Index;
            updateSelectedGlassExamination(this.mSelectedGlassesExamination);
            mDBManager.Update(this.mSelectedGlassesExamination);

            this.glassesExaminationTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesExamination);
            this.glassesExaminationDataGridView.ClearSelection();
            this.glassesExaminationDataGridView.Rows[updatedRowIndex].Selected = true;

        }

        private void updateGlassesOrderButton_Click(object sender, EventArgs e)
        {
            int updatedRowIndex = this.mSelectedGlassesOrderRow.Index;
            updateSelectedGlassOrder(this.mSelectedGlassesOrder);
            mDBManager.UpdateGlassOrder(this.mSelectedGlassesOrder);

            this.glassesOrderTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesOrder);
            this.glassesOrderDataGridView.ClearSelection();
            this.glassesOrderDataGridView.Rows[updatedRowIndex].Selected = true;

        }

        private void updateLensesExaminationButton_Click(object sender, EventArgs e)
        {
            int updatedRowIndex = this.mSelectedLensesExaminationRow.Index;
            updateSelectedLensesExamination(this.mSelectedLensesExamination);
            mDBManager.Update(this.mSelectedLensesExamination);

            this.lensesExaminationTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.LensesExamination);
            this.lensesExaminationDataGridView.ClearSelection();
            this.lensesExaminationDataGridView.Rows[updatedRowIndex].Selected = true;
        }

        private void updateLensesOrderButton_Click(object sender, EventArgs e)
        {
            int updatedRowIndex = this.mSelectedLensesOrderRow.Index;
            updateSelectedLensesOrder(this.mSelectedLensesOrder);
            mDBManager.UpdateLensesOrder(this.mSelectedLensesOrder);

            this.lensesOrderTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.LensesOrder);
            this.lensesOrderDataGridView.ClearSelection();
            this.lensesOrderDataGridView.Rows[updatedRowIndex].Selected = true;
        }

        private void addnewClientButton_Click(object sender, EventArgs e)
        {
            ClientInfo newClient = new ClientInfo();
            updateSelectedClientInfo(newClient);
            mDBManager.Insert(newClient);

            this.clientInfoTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.ClientInfo);
            this.clientsInfoDataGridView.ClearSelection();
            int tableSize = clientsInfoDataGridView.Rows.Count;
            this.clientsInfoDataGridView.Rows[tableSize - 1].Selected = true;


        }

        private void addnewGlassesExaminationButton_Click(object sender, EventArgs e)
        {
            GlasesExamination newGlasesExamination = new GlasesExamination();

            updateSelectedGlassExamination(newGlasesExamination);
            mDBManager.Insert(newGlasesExamination);

            // this.glassesExaminationTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesExamination);
            updateGlassesExaminationControls(true);
            this.glassesExaminationDataGridView.ClearSelection();
            int tableSize = this.glassesExaminationDataGridView.Rows.Count;
            if (tableSize > 0)
                this.glassesExaminationDataGridView.Rows[tableSize - 1].Selected = true;

           

        }

        private void addnewGlassesOrderButton_Click(object sender, EventArgs e)
        {

            Order newGlasesOrder = new Order();

            updateSelectedGlassOrder(newGlasesOrder);
            mDBManager.InsertGlassOrder(newGlasesOrder);


            // this.glassesOrderTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesOrder);
            this.glassesOrderDataGridView.ClearSelection();
            int tableSize = this.glassesOrderDataGridView.Rows.Count;
            this.glassesOrderDataGridView.Rows[tableSize - 1].Selected = true;
        }

        private void addnewLensesExaminationButton_Click(object sender, EventArgs e)
        {
            LensesExamination newLensesExamination = new LensesExamination();

            updateSelectedLensesExamination(newLensesExamination);
            mDBManager.Insert(newLensesExamination);

            // this.glassesExaminationTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesExamination);

            this.lensesExaminationDataGridView.ClearSelection();
            int tableSize = this.lensesExaminationDataGridView.Rows.Count;
            if (tableSize > 0)
                this.lensesExaminationDataGridView.Rows[tableSize - 1].Selected = true;
        }

        private void addnewLensesOrderButton_Click(object sender, EventArgs e)
        {

            Order newLensesOrder = new Order();

            updateSelectedLensesOrder(newLensesOrder);
            mDBManager.InsertLensesOrder(newLensesOrder);


            // this.glassesOrderTableAdapter.Fill(this._CVC_DatabaseDataSet_Form.GlassesOrder);
            this.lensesOrderDataGridView.ClearSelection();
            int tableSize = this.lensesOrderDataGridView.Rows.Count;
            this.lensesOrderDataGridView.Rows[tableSize - 1].Selected = true;
        }
        
        //help methods


        private void updateClientControls(Boolean state)
        {
            if (state == false)
            {
                this.mSelectedClientInfo = null;
                this.mSelectedClientRow = null;
            }

            this.updateClientInfoButton.Enabled = state;
            this.glassesExaminationTabPage.Enabled = state;
            this.lensesExaminationTabPage.Enabled = state;
            updateClientInfoPanelTextBox();
            fillGlassesExaminationGridView(state);
            fillLensesExaminationGridView(state);
        }

        private void updateGlassesExaminationControls(Boolean state)
        {
            if (state == false)
            {
                this.mSelectedGlassesExamination = null;
                this.mSelectedGlassesExaminationRow = null;
            }

            this.updateGlassesExaminationButton.Enabled = state;
            this.eyeGlassesTabPage.Enabled = state;

            updateGlassesExaminationPanelTextBox();
            fillGlassesOrderGridView(state);
        }

        private void updateGlassesOrderControls(Boolean state)
        {
            if (state == false)
            {
                this.mSelectedGlassesOrder = null;
                this.mSelectedGlassesOrderRow = null;
            }

            this.updateGlassesOrderButton.Enabled = state;
            updateGlassesOrderPanelTextBox();
        }

        private void updateLensesExaminationControls(Boolean state)
        {
            if (state == false)
            {
                this.mSelectedLensesExamination = null;
                this.mSelectedLensesExaminationRow = null;
            }

            this.updateLensesExaminationButton.Enabled = state;
            this.lensesTabPage.Enabled = state;

            updateLensesExaminationPanelTextBox();
            fillLensesOrderGridView(state);
        }

        private void updateLensesOrderControls(Boolean state)
        {
            if (state == false)
            {
                this.mSelectedLensesOrder = null;
                this.mSelectedLensesOrderRow = null;
            }

            this.updateLensesOrderButton.Enabled = state;
            updateLensesOrderPanelTextBox();
        }

        private ClientInfo ParseClientInfo(DataGridViewRow row)
		{
			ClientInfo retval = new ClientInfo();
			int i = 0;
			foreach (ClientInfo.Fields clientField in Enum.GetValues(typeof(ClientInfo.Fields)))
			{
				Object cellValue = row.Cells[i++].Value;
				if (Convert.IsDBNull(cellValue) == true)
					cellValue = null;
				retval[clientField] = cellValue;
			}
			return retval;
		}

		private GlasesExamination ParseGlassesExamination(DataGridViewRow row)
		{
			GlasesExamination retval = new GlasesExamination();
			int i = 0;
            ExaminationElement examinationElement;
            Object cellValue;
            for (i = 1; i < 17; i+=2)
            {
                double osValue=0, odValue=0;
                examinationElement = new ExaminationElement();


                cellValue = row.Cells[i].Value;
                if (Convert.IsDBNull(cellValue) == false)
                    odValue = (double)cellValue;

                cellValue = row.Cells[i+1].Value;
                if (Convert.IsDBNull(cellValue) == false)
                    osValue = (double)cellValue;
                
                
                examinationElement[ExaminationElement.Fields.OS] = osValue;
                examinationElement[ExaminationElement.Fields.OD] = odValue;

                Examination.Fields examinationField = (Examination.Fields)((i - 1)/2);
                retval[examinationField] = examinationElement;
            }

            cellValue = row.Cells[0].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Examination.Fields.Date] = cellValue;

            cellValue = row.Cells[17].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Examination.Fields.ClientID] = cellValue;
            
            cellValue = row.Cells[18].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Examination.Fields.ID] = cellValue;

			return retval;
		}

        private Order ParseGlassesOrder(DataGridViewRow row)
        {
            Order retval = new Order();
            int i = 0;
            EyeGlasses eyeGlasses = new EyeGlasses();
            Object cellValue;
            EyeGlasses.Fields eyeglassesField;
           
            for (i = 2; i < 17; i++)
            {

                eyeglassesField = (EyeGlasses.Fields)(i - 2);
                cellValue = row.Cells[i].Value;
                eyeGlasses[eyeglassesField] = (Boolean)cellValue;
            }
            cellValue = row.Cells[17].Value;
            if (Convert.IsDBNull(cellValue) == false)
                eyeGlasses[EyeGlasses.Fields.Frame] = cellValue;
            else
                eyeGlasses[EyeGlasses.Fields.Frame] = null;

            retval[Order.Fields.OrderElement] = eyeGlasses;

            cellValue = row.Cells[0].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.OrderDate] = cellValue;
            else
                retval[Order.Fields.OrderDate] = null;

            cellValue = row.Cells[1].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.Price] = cellValue;
            else
                retval[Order.Fields.Price] = null;

            cellValue = row.Cells[18].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.ID] = cellValue;
            else
                retval[Order.Fields.ID] = 0;

            cellValue = row.Cells[19].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.ClientID] = cellValue;
            else
                retval[Order.Fields.ClientID] = 0;

            cellValue = row.Cells[20].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.ExaminationID] = cellValue;
            else
                retval[Order.Fields.ExaminationID] = 0;


            return retval;
        }

        private LensesExamination ParseLensesExamination(DataGridViewRow row)
        {
            LensesExamination retval = new LensesExamination();
            int i = 0;
            ExaminationElement examinationElement;
            Object cellValue;
            for (i = 1; i < 13; i += 2)
            {
                double osValue = 0, odValue = 0;
                examinationElement = new ExaminationElement();


                cellValue = row.Cells[i].Value;
                if (Convert.IsDBNull(cellValue) == false)
                    odValue = Convert.ToDouble(cellValue);

                cellValue = row.Cells[i + 1].Value;
                if (Convert.IsDBNull(cellValue) == false)
                    osValue = Convert.ToDouble(cellValue);


                examinationElement[ExaminationElement.Fields.OS] = osValue;
                examinationElement[ExaminationElement.Fields.OD] = odValue;

                Examination.Fields examinationField = Examination.Fields.ID;
                if(i < 7)
                    examinationField = (Examination.Fields)((i - 1) / 2);
                if (i == 7 )
                   examinationField = (Examination.Fields)(i + 1);
                if (i == 9)
                    examinationField = (Examination.Fields)(i);
                if (i == 11)
                    examinationField = (Examination.Fields)(i - 1);

                retval[examinationField] = examinationElement;
            }

            cellValue = row.Cells[0].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Examination.Fields.Date] = cellValue;

            cellValue = row.Cells[13].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Examination.Fields.ID] = cellValue;

            cellValue = row.Cells[14].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Examination.Fields.ClientID] = cellValue;

            return retval;
        }

        private Order ParseLensesOrder(DataGridViewRow row)
        {
            Order retval = new Order();
            Lenses lenses = new Lenses();
            Object cellValue;

            cellValue = row.Cells[0].Value;
            if (Convert.IsDBNull(cellValue) == false)
                lenses[Lenses.Fields.Type] = (Lenses.Types)(Enum.Parse(typeof(Lenses.Types), (string)cellValue));
            else
                lenses[Lenses.Fields.Type] = null;

            cellValue = row.Cells[1].Value;
            if (Convert.IsDBNull(cellValue) == false)
                lenses[Lenses.Fields.Brand] = (Lenses.Brands)(Enum.Parse(typeof(Lenses.Brands), (string)cellValue));
            else
                lenses[Lenses.Fields.Brand] = null;

            retval[Order.Fields.OrderElement] = lenses;

            cellValue = row.Cells[2].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.Price] = cellValue;
            else
                retval[Order.Fields.Price] = null;

            cellValue = row.Cells[3].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.OrderDate] = cellValue;
            else
                retval[Order.Fields.OrderDate] = null;


            cellValue = row.Cells[4].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.ExaminationID] = cellValue;
            else
                retval[Order.Fields.ExaminationID] = 0;

            cellValue = row.Cells[5].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.ClientID] = cellValue;
            else
                retval[Order.Fields.ClientID] = 0;

            cellValue = row.Cells[6].Value;
            if (Convert.IsDBNull(cellValue) == false)
                retval[Order.Fields.ID] = cellValue;
            else
                retval[Order.Fields.ID] = 0;

            return retval;
        }

        private void updateClientInfoPanelTextBox()
        {
            if (this.mSelectedClientInfo == null)
            {
                firstNameTextBox.Clear();
                lastNameTextBox.Clear();
                mobileTextBox.Clear();
                telTextBox.Clear();
                addressTextBox.Clear();
                emailTextBox.Clear();
                membershipIDTextBox.Clear();
                //genderComboBox.
                dOBdateTimePicker.Value = DateTime.Now;


            }
            else
            {
                Object value = null;

                value = mSelectedClientInfo[ClientInfo.Fields.FirstName];
                value = value == null ? "" : value;
                firstNameTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.LastName];
                value = value == null ? "" : value;
                lastNameTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.Address];
                value = value == null ? "" : value;
                addressTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.Tel];
                value = value == null ? "" : value;
                telTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.Mobile];
                value = value == null ? "" : value;
                mobileTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.Email];
                value = value == null ? "" : value;
                emailTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.Gender];
                value = value == null ? "" : value;
                if (value != null)
                {
                    if ((ClientInfo.GenderEnum)value == ClientInfo.GenderEnum.Male)
                        genderComboBox.SelectedItem = genderComboBox.Items[0];
                    else
                        genderComboBox.SelectedItem = genderComboBox.Items[1];
                }
                // genderTextBox.Text = value.ToString();

                value = mSelectedClientInfo[ClientInfo.Fields.DateOfBirth];
                value = value == null ? "" : value;
                // dateOfBirthTextBox.Text = ((DateTime)value).ToString("dd/MM/yyyy");
                dOBdateTimePicker.Value = (DateTime)value;

                value = mSelectedClientInfo[ClientInfo.Fields.MembershipID];
                value = value == null ? "" : value;
                membershipIDTextBox.Text = value.ToString();



                //foreach (KeyValuePair<ClientInfo.Fields, System.Windows.Forms.TextBox> pair in mClientInfoPanelTextBoxs)
                //{
                //    Object value = mSelectedClientInfo[pair.Key];
                //    value = value == null ? "" : value;
                //    pair.Value.Text = value.ToString();
                //}
                //if (mSelectedClientInfo[ClientInfo.Fields.DateOfBirth] != null)
                //{
                ////    this.dateOfBirthTextBox.Text = ((DateTime)(mSelectedClientInfo[ClientInfo.Fields.DateOfBirth])).ToString("dd/MM/yyyy");
                //}
            }
        }
		
        private void updateGlassesExaminationPanelTextBox()
		{
            if (this.mSelectedGlassesExamination == null)
            {
                this.sphereOSTextBox.Clear();
                this.sphereODTextBox.Clear();

                this.cylinderOSTextBox.Clear();
                this.cylinderODTextBox.Clear();

                this.axisOSTextBox.Clear();
                this.axisODTextBox.Clear();

                this.prisimOSTextBox.Clear();
                this.prisimODTextBox.Clear();

                this.baseOSTextBox.Clear();
                this.baseODTextBox.Clear();

                this.addOSTextBox.Clear();
                this.addODTextBox.Clear();

                this.hOSTextBox.Clear();
                this.hODTextBox.Clear();

                this.pdOSTextBox.Clear();
                this.pdODTextBox.Clear();

                this.glassesExaminationDateTimePicker.Value = DateTime.Now;


            }
            else
            {
                Object value = null;

                value = mSelectedGlassesExamination[Examination.Fields.Date];
                value = value == null ? "" : value;
                glassesExaminationDateTimePicker.Value = (DateTime)value;

                ExaminationElement examination = null;
                double examinationValue = 0;

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.SPHERE]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        sphereOSTextBox.Text = examinationValue.ToString();
                    else
                        sphereOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        sphereODTextBox.Text = examinationValue.ToString();
                    else
                        sphereODTextBox.Text = "";
                }
                else
                {
                    sphereOSTextBox.Text = sphereODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.CYLINDER]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        cylinderOSTextBox.Text = examinationValue.ToString();
                    else
                        cylinderOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        cylinderODTextBox.Text = examinationValue.ToString();
                    else
                        cylinderODTextBox.Text = "";
                }
                else
                {
                    cylinderOSTextBox.Text = cylinderODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.AXIS]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        axisOSTextBox.Text = examinationValue.ToString();
                    else
                        axisOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        axisODTextBox.Text = examinationValue.ToString();
                    else
                        axisODTextBox.Text = "";
                }
                else
                {
                    axisOSTextBox.Text = axisODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.PRISM]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        prisimOSTextBox.Text = examinationValue.ToString();
                    else
                        prisimOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        prisimODTextBox.Text = examinationValue.ToString();
                    else
                        prisimODTextBox.Text = "";
                }
                else
                {
                    prisimOSTextBox.Text = prisimODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.BASE]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        baseOSTextBox.Text = examinationValue.ToString();
                    else
                        baseOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        baseODTextBox.Text = examinationValue.ToString();
                    else
                        baseODTextBox.Text = "";
                }
                else
                {
                    baseOSTextBox.Text = baseODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.ADD]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        addOSTextBox.Text = examinationValue.ToString();
                    else
                        addOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        addODTextBox.Text = examinationValue.ToString();
                    else
                        addODTextBox.Text = "";
                }
                else
                {
                    addOSTextBox.Text = addODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.H]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        hOSTextBox.Text = examinationValue.ToString();
                    else
                        hOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        hODTextBox.Text = examinationValue.ToString();
                    else
                        hODTextBox.Text = "";
                }
                else
                {
                    hOSTextBox.Text = hODTextBox.Text = "";
                }

                examination = (ExaminationElement)(mSelectedGlassesExamination[Examination.Fields.PD]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        pdOSTextBox.Text = examinationValue.ToString();
                    else
                        pdOSTextBox.Text = "";

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        pdODTextBox.Text = examinationValue.ToString();
                    else
                        pdODTextBox.Text = "";
                }
                else
                {
                    pdOSTextBox.Text = pdODTextBox.Text = "";
                }
            }
		}

        private void updateGlassesOrderPanelTextBox()
        {
            if (this.mSelectedGlassesOrder == null)
            {
                glassesOrderDateTimePicker.Value = DateTime.Now;
                priceTextBox.Clear();
                frameTextBox.Clear();
                glassCheckBox.Checked = false;
                cr39CheckBox.Checked = false;
                uv400CheckBox.Checked = false; 
                biFocalCheckBox.Checked = false;
                progressiveCheckBox.Checked = false;
                triFocalCheckBox.Checked = false;
                cr39ArCheckBox.Checked = false;
                tntPgxCheckBox.Checked = false;
                polyCarbonCheckBox.Checked = false;
                arCoatingCheckBox.Checked = false;
                singalVisionCheckBox.Checked = false;
                hiIndex156CheckBox.Checked = false;
                hiIndex160CheckBox.Checked = false;
                hiIndex166CheckBox.Checked = false;
                hiIndex174CheckBox.Checked = false;
            }
            else
            {
                Object value = null;

                value = mSelectedGlassesOrder[Order.Fields.OrderDate];
                if (value != null)
                    glassesOrderDateTimePicker.Value = (DateTime)value;

                value = mSelectedGlassesOrder[Order.Fields.Price];
                value = value == null ? "" : value;
                priceTextBox.Text = value.ToString();

                EyeGlasses eyeglass = (EyeGlasses)(mSelectedGlassesOrder[Order.Fields.OrderElement]);

                value = eyeglass[EyeGlasses.Fields.Frame];
                value = value == null ? "" : value;
                frameTextBox.Text = value.ToString();

                glassCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Glass]);

                cr39CheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.CR_39]);

                uv400CheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.UV_400]);

                biFocalCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Bi_Focal]);

                progressiveCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Progressive]);

                triFocalCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.TriFocal]);

                cr39ArCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.CR_39_AR]);

                tntPgxCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Tint_PGX]);

                polyCarbonCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Poly_Carbon]);

                arCoatingCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.AR_Coating]);

                singalVisionCheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Singal_Vision]);

                hiIndex156CheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Hi_Index_156]);

                hiIndex160CheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Hi_Index_160]);

                hiIndex166CheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Hi_Index_166]);

                hiIndex174CheckBox.Checked = (Boolean)(eyeglass[EyeGlasses.Fields.Hi_Index_174]);

            }

        }

        private void updateLensesExaminationPanelTextBox()
        {
            if (this.mSelectedLensesExamination == null)
            {
                this.lensesSphereOSTextBox.Clear();
                this.lensesSphereODTextBox.Clear();

                this.lensesCylinderOSTextBox.Clear();
                this.lensesCylinderODTextBox.Clear();

                this.lensesAxisOSTextBox.Clear();
                this.lensesAxisODTextBox.Clear();

                this.lensesBcOSTextBox.Clear();
                this.lensesBcODTextBox.Clear();

                this.lensesDiaOSTextBox.Clear();
                this.lensesDiaODTextBox.Clear();

                this.lensesKrOSTextBox.Clear();
                this.lensesKrODTextBox.Clear();

                this.lensesExaminationDateTimePicker.Value = DateTime.Now;


            }
            else
            {
                Object value = null;

                value = mSelectedLensesExamination[Examination.Fields.Date];
                value = value == null ? "" : value;
                lensesExaminationDateTimePicker.Value = (DateTime)value;

                ExaminationElement examination = null;
                double examinationValue = 0;

                examination = (ExaminationElement)(mSelectedLensesExamination[Examination.Fields.SPHERE]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        lensesSphereOSTextBox.Text = examinationValue.ToString();
                    else
                        lensesSphereOSTextBox.Clear();

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        lensesSphereODTextBox.Text = examinationValue.ToString();
                    else
                        lensesSphereODTextBox.Clear();
                }
                else
                {
                    lensesSphereOSTextBox.Clear(); lensesSphereODTextBox.Clear();
                }

                examination = (ExaminationElement)(mSelectedLensesExamination[Examination.Fields.CYLINDER]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        lensesCylinderOSTextBox.Text = examinationValue.ToString();
                    else
                        lensesCylinderOSTextBox.Clear();

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        lensesCylinderODTextBox.Text = examinationValue.ToString();
                    else
                        lensesCylinderODTextBox.Clear();
                }
                else
                {
                    lensesCylinderOSTextBox.Clear(); lensesCylinderODTextBox.Clear();
                }

                examination = (ExaminationElement)(mSelectedLensesExamination[Examination.Fields.AXIS]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        lensesAxisOSTextBox.Text = examinationValue.ToString();
                    else
                        lensesAxisOSTextBox.Clear();

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        lensesAxisODTextBox.Text = examinationValue.ToString();
                    else
                        lensesAxisODTextBox.Clear();
                }
                else
                {
                    lensesAxisOSTextBox.Clear(); lensesAxisODTextBox.Clear();
                }

                examination = (ExaminationElement)(mSelectedLensesExamination[Examination.Fields.BC]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        lensesBcOSTextBox.Text = examinationValue.ToString();
                    else
                        lensesBcOSTextBox.Clear();

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        lensesBcODTextBox.Text = examinationValue.ToString();
                    else
                        lensesBcODTextBox.Clear();
                }
                else
                {
                    lensesBcOSTextBox.Clear(); lensesBcODTextBox.Clear();
                }

                examination = (ExaminationElement)(mSelectedLensesExamination[Examination.Fields.DIA]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        lensesDiaOSTextBox.Text = examinationValue.ToString();
                    else
                        lensesDiaOSTextBox.Clear();

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        lensesDiaODTextBox.Text = examinationValue.ToString();
                    else
                        lensesDiaODTextBox.Clear();
                }
                else
                {
                    lensesDiaOSTextBox.Clear(); lensesDiaODTextBox.Clear();
                }

                examination = (ExaminationElement)(mSelectedLensesExamination[Examination.Fields.KR]);
                if (examination != null)
                {
                    examinationValue = examination[ExaminationElement.Fields.OS];
                    if (examinationValue != 0)
                        lensesKrOSTextBox.Text = examinationValue.ToString();
                    else
                        lensesKrOSTextBox.Clear();

                    examinationValue = examination[ExaminationElement.Fields.OD];
                    if (examinationValue != 0)
                        lensesKrODTextBox.Text = examinationValue.ToString();
                    else
                        lensesKrODTextBox.Clear();
                }
                else
                {
                    lensesKrOSTextBox.Clear(); lensesKrODTextBox.Clear();
                }

               
            }
        }

        private void updateLensesOrderPanelTextBox()
        {
            if (this.mSelectedLensesOrder == null)
            {
                lensesOrderDateTimePicker.Value = DateTime.Now;
                lensesPriceTextBox.Clear();
                frameTextBox.Clear();
            }
            else
            {
                Object value = null;

                value = mSelectedLensesOrder[Order.Fields.OrderDate];
                if (value != null)
                    lensesOrderDateTimePicker.Value = (DateTime)value;

                value = mSelectedLensesOrder[Order.Fields.Price];
                value = value == null ? "" : value;
                lensesPriceTextBox.Text = value.ToString();

                Lenses lenses = (Lenses)(mSelectedLensesOrder[Order.Fields.OrderElement]);

                value = lenses[Lenses.Fields.Type];
                if(value !=null)
                    lensesTypeComboBox.SelectedItem = lensesTypeComboBox.Items[(int)value];

                value = lenses[Lenses.Fields.Brand];
                if (value != null)
                    lensesBrandComboBox.SelectedItem = lensesBrandComboBox.Items[(int)value];
                

            }

        }
        
        void updateSelectedClientInfo(ClientInfo clientInfo)
		{
			if (clientInfo == null)
				return;
			clientInfo[ClientInfo.Fields.FirstName] = firstNameTextBox.Text.Trim();
			clientInfo[ClientInfo.Fields.LastName] = lastNameTextBox.Text.Trim();

			if (addressTextBox.Text != null && addressTextBox.Text.Trim() != "")
				clientInfo[ClientInfo.Fields.Address] = addressTextBox.Text.Trim();

			if (telTextBox.Text != null && telTextBox.Text.Trim() != "")
				clientInfo[ClientInfo.Fields.Tel] = telTextBox.Text.Trim();

			if (mobileTextBox.Text != null && mobileTextBox.Text.Trim() != "")
				clientInfo[ClientInfo.Fields.Mobile] = mobileTextBox.Text.Trim();

			if (emailTextBox.Text != null && emailTextBox.Text.Trim() != "")
				clientInfo[ClientInfo.Fields.Email] = emailTextBox.Text.Trim();

			if (genderComboBox.SelectedItem != null)
				clientInfo[ClientInfo.Fields.Gender] = genderComboBox.SelectedItem.ToString();

			if (dOBdateTimePicker.Value != null)
				clientInfo[ClientInfo.Fields.DateOfBirth] = dOBdateTimePicker.Value;

			if (membershipIDTextBox.Text != null && membershipIDTextBox.Text.Trim() != "")
				clientInfo[ClientInfo.Fields.MembershipID] = Convert.ToInt32(membershipIDTextBox.Text);
		}

        void updateSelectedGlassExamination(GlasesExamination glasesExamination)
        {
            if (glasesExamination == null)
                return;
            glasesExamination[GlasesExamination.Fields.ClientID] = mSelectedClientInfo[ClientInfo.Fields.ID];
            glasesExamination[GlasesExamination.Fields.Date] = glassesExaminationDateTimePicker.Value;


            ExaminationElement examinationElement;
            string value;

            examinationElement = new ExaminationElement();
            value = sphereOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = sphereODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.SPHERE] = examinationElement;

            examinationElement = new ExaminationElement();
            value = cylinderOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = cylinderODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.CYLINDER] = examinationElement;

            examinationElement = new ExaminationElement();
            value = axisOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = axisODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.AXIS] = examinationElement;

            examinationElement = new ExaminationElement();
            value = prisimOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = prisimODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.PRISM] = examinationElement;

            examinationElement = new ExaminationElement();
            value = baseOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = baseODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.BASE] = examinationElement;


            examinationElement = new ExaminationElement();
            value = addOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = addODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.ADD] = examinationElement;


            examinationElement = new ExaminationElement();
            value = hOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = hODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.H] = examinationElement;

            examinationElement = new ExaminationElement();
            value = pdOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = pdODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            glasesExamination[GlasesExamination.Fields.PD] = examinationElement;


        }

        void updateSelectedGlassOrder(Order glassesOrder)
        {
            if (glassesOrder == null)
                return;

            glassesOrder[Order.Fields.ClientID] = mSelectedClientInfo[ClientInfo.Fields.ID];
            glassesOrder[Order.Fields.ExaminationID] = mSelectedGlassesExamination[Examination.Fields.ID];
            glassesOrder[Order.Fields.OrderDate] = glassesOrderDateTimePicker.Value;

            object value = priceTextBox.Text;
            if (value != null && value.ToString().Trim() != "")
            {
                value = Convert.ToInt64(value.ToString().Trim());
            }
            else
                value = 0;
            glassesOrder[Order.Fields.Price] = value;

            EyeGlasses eyeGlasses = new EyeGlasses();

            value = frameTextBox.Text;
            if (value != null)
                value = value.ToString().Trim();
            eyeGlasses[EyeGlasses.Fields.Frame] = value;

            eyeGlasses[EyeGlasses.Fields.Glass] = glassCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.CR_39] = cr39CheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.UV_400] = uv400CheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Bi_Focal] = biFocalCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Progressive] = progressiveCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.TriFocal] = triFocalCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.CR_39_AR] = cr39ArCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Tint_PGX] = tntPgxCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Poly_Carbon] = polyCarbonCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.AR_Coating] = arCoatingCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Singal_Vision] = singalVisionCheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Hi_Index_156] = hiIndex156CheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Hi_Index_160] = hiIndex160CheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Hi_Index_166] = hiIndex166CheckBox.Checked;

            eyeGlasses[EyeGlasses.Fields.Hi_Index_174] = hiIndex174CheckBox.Checked;

            glassesOrder[Order.Fields.OrderElement] = eyeGlasses;
            

        }

        void updateSelectedLensesExamination(LensesExamination lensesExamination)
        {
            if (lensesExamination == null)
                return;
            lensesExamination[LensesExamination.Fields.ClientID] = mSelectedClientInfo[ClientInfo.Fields.ID];
            lensesExamination[LensesExamination.Fields.Date] = lensesExaminationDateTimePicker.Value;


            ExaminationElement examinationElement;
            string value;

            examinationElement = new ExaminationElement();
            value = lensesSphereOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = lensesSphereODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            lensesExamination[LensesExamination.Fields.SPHERE] = examinationElement;

            examinationElement = new ExaminationElement();
            value = lensesCylinderOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = lensesCylinderODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            lensesExamination[LensesExamination.Fields.CYLINDER] = examinationElement;

            examinationElement = new ExaminationElement();
            value = lensesAxisOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = lensesAxisODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            lensesExamination[LensesExamination.Fields.AXIS] = examinationElement;

            examinationElement = new ExaminationElement();
            value = lensesBcOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = lensesBcODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            lensesExamination[LensesExamination.Fields.BC] = examinationElement;

            examinationElement = new ExaminationElement();
            value = lensesDiaOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = lensesDiaODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            lensesExamination[LensesExamination.Fields.DIA] = examinationElement;

            examinationElement = new ExaminationElement();
            value = lensesKrOSTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OS] = Convert.ToDouble(value);
            value = lensesKrODTextBox.Text;
            if (value != null && value.Trim() != "")
                examinationElement[ExaminationElement.Fields.OD] = Convert.ToDouble(value);
            lensesExamination[LensesExamination.Fields.DIA] = examinationElement;

        }

        void updateSelectedLensesOrder(Order lensesOrder)
        {
            if (lensesOrder == null)
                return;

            lensesOrder[Order.Fields.ClientID] = mSelectedClientInfo[ClientInfo.Fields.ID];
            lensesOrder[Order.Fields.ExaminationID] = mSelectedLensesExamination[Examination.Fields.ID];
            lensesOrder[Order.Fields.OrderDate] = lensesOrderDateTimePicker.Value;

            object value = lensesPriceTextBox.Text;
            if (value != null && value.ToString().Trim() != "")
            {
                value = Convert.ToInt64(value.ToString().Trim());
            }
            else
                value = 0;
            lensesOrder[Order.Fields.Price] = value;

            Lenses lenses = new Lenses();

            value = lensesTypeComboBox.SelectedItem;
            if (value != null)
                value = Enum.Parse(typeof(Lenses.Types), value.ToString());
            else
                value = null;
            lenses[Lenses.Fields.Type] = (Lenses.Types)value;

            value = lensesBrandComboBox.SelectedItem;
            if (value != null)
                value = Enum.Parse(typeof(Lenses.Brands), value.ToString());
            else
                value = null;
            lenses[Lenses.Fields.Brand] = (Lenses.Brands)value;
            
            lensesOrder[Order.Fields.OrderElement] = lenses;


        }

        private void fillGlassesExaminationGridView(Boolean state)
        {
            if (state == false)
            {
                this._CVC_DatabaseDataSet_Form.GlassesExamination.Clear();
            }
            else
            {
                int selectedClientId = 0;
                if (this.mSelectedClientInfo != null)
                    selectedClientId = (int)(this.mSelectedClientInfo[ClientInfo.Fields.ID]);

                this.glassesExaminationTableAdapter.FillBySelectedClientID(this._CVC_DatabaseDataSet_Form.GlassesExamination, selectedClientId);
            }
        }

        private void fillGlassesOrderGridView(Boolean state)
        {
            if (state == false)
            {
                this._CVC_DatabaseDataSet_Form.GlassesOrder.Clear();
            }
            else
            {
                int selectedExaminationID = 0;
                if (this.mSelectedGlassesExamination != null)
                    selectedExaminationID = (int)(this.mSelectedGlassesExamination[Examination.Fields.ID]);

                this.glassesOrderTableAdapter.FillByExaminatioinID(this._CVC_DatabaseDataSet_Form.GlassesOrder, selectedExaminationID);
            }

        }

        private void fillLensesExaminationGridView(Boolean state)
        {
            if (state == false)
            {
                this._CVC_DatabaseDataSet_Form.LensesExamination.Clear();
            }
            else
            {
                int selectedClientId = 0;
                if (this.mSelectedClientInfo != null)
                    selectedClientId = (int)(this.mSelectedClientInfo[ClientInfo.Fields.ID]);

                this.lensesExaminationTableAdapter.FillByClientID(this._CVC_DatabaseDataSet_Form.LensesExamination, selectedClientId);

            }
        }

        private void fillLensesOrderGridView(Boolean state)
        {
            if (state == false)
            {
                this._CVC_DatabaseDataSet_Form.LensesOrder.Clear();
            }
            else
            {
                int selectedExaminationID = 0;
                if (this.mSelectedLensesExamination != null)
                    selectedExaminationID = (int)(this.mSelectedLensesExamination[LensesExamination.Fields.ID]);                
                this.lensesOrderTableAdapter.FillByExaminationID(this._CVC_DatabaseDataSet_Form.LensesOrder, selectedExaminationID);
            }
        }

       

       

       
        
     
        
     

       
	}
}